<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 */
require_once(LIB_PATH.'phpMailer/class.phpmailer.php');


/**
 * Classe permettant de gerer les acheteurs , suavagarde leur critere apres avoir consulter la fiche d'une annonce
 *
 */
class acheteurSrv {


    /**
     * Fonction permettant d'ajouter un acheteur
     * @return object $acheteur
     *
     */
    public static function ajouter ($acheteur) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_acheteur");
        $records   = jDao::createRecord("commun~ao_acheteur");

        $records->marqueId      = $acheteur->marqueId;
        $records->modeleId      = $acheteur->modeleId;
        $records->mail          = $acheteur->mail;
        
        return $maFactory->insert($records);

    }


    /**
     * Envoi de mail au vendeur et en copie le site partenaire
     *
     */
     public static function sendMail($datas) {
        $mail = new PHPmailer();
        $mail->IsSMTP();
        $mail->IsHtml(true);
        $mail->AddAddress($datas->vendeurMail);
        $mail->Host         = $GLOBALS['gJConfig']->mailer['smtpHost'];
        $mail->Port         = $GLOBALS['gJConfig']->mailer['smtpPort'];
        $mail->Username     = 'Auto Occasion';
        $mail->From         = $GLOBALS['gJConfig']->mailer['webmasterEmail'];
        $mail->FromName     = 'Auto - occasion';
        $mail->Subject      = 'Auto Occasion : Annonce';
    
        $mail->Body         = $datas->message ;
        
        if (!$mail->Send()){
          echo $mail->ErrorInfo;
          die('Sending mail - Error');
        }
        
        $mail->SmtpClose();
        $mail->ClearAddresses();
        unset($mail);
        
        //transit mail
        self::transitMail($datas);
     }
     
     
    /**
     * Envoi de mail à un ami
     *
     */
     public static function sendMailToFriend($datas,$dest) {
        $mail = new PHPmailer();
        $mail->IsSMTP();
        $mail->IsHtml(true);
        $mail->AddAddress($dest->adress);
        $mail->Host         = $GLOBALS['gJConfig']->mailer['smtpHost'];
        $mail->Port         = $GLOBALS['gJConfig']->mailer['smtpPort'];
        $mail->Username     = 'Auto Occasion';
        $mail->From         = $datas->adress;
        $mail->FromName     = 'Auto - occasion ('. $datas->adress .')';
        $mail->Subject      = 'Auto Occasion : Annonce';
    
        $mail->Body         = $datas->message ;
        
        if (!$mail->Send()){
          echo $mail->ErrorInfo;
          die('Sending mail - Error');
        }
        
        $mail->SmtpClose();
        $mail->ClearAddresses();
        unset($mail);
        
       
     }

     /**
      * transit mail : envoi une copie du mail destiné au vendeur 
      * @param object $datas : données à envoyer
      */
      public static function transitMail ($datas) {
            $email   = urlencode($datas->vendeurMail);
            $nom     = urlencode($datas->nom);
            $prenom  = urlencode($datas->prenom);
            $tel1    = urlencode($datas->tel);
            $message = urlencode($datas->message);
            $id_annonce = urlencode($datas->idAnnnonce); //id => identifiant d'origine de l'annonce (celui sur Auto-Selection)

            $url ="http://autooccasion:zCQt4ew@secure.auto-selection.com/partnerApi/scripts/mailTransit.php?id=$id_annonce&nom=$nom&prenom=$prenom&tel1=$tel1&email=$email&partnerID=ao";


            if( !$fp = fopen($url, "r") ) self::sendMail($datas); // router directement le message à l'adresse de contact de l'annonce concernée.
            fclose($fp);

      }

}